/**
 * 泛型
 * 标注的具体类型并不能确定
 */

{
  // 函数

  function getVal<T>(obj: T, k: keyof T) {
    return obj[k]
  }

  let obj1 = {
    x: 1,
    y: 2
  }
  let obj2 = {
    username: 'zmouse',
    age: 35
  }

  // 这里就出现了不确定性
  getVal<typeof obj1>(obj1, 'x');
  getVal<typeof obj2>(obj2, 'age');


  // 泛型类

  // 泛型接口
  interface IResponseData<T> {
    code: number;
    message?: string;
    data: T
  }

  // 用户接口
  interface IResponseUserData {
    id: number;
    username: string;
    email: string;
  }

  // 文章接口
  interface IResponseArticleData {
    id: number;
    title: string;
    author: IResponseUserData
  }

  async function getData<U>(url: string) {
    let response = await fetch(url);
    let data: Promise<IResponseData<U>> = await response.json();
    return data;
  }

  (async function () {
    let userData = await getData<IResponseUserData>('/user');
    userData.data.id;

    let articleData = await getData<IResponseArticleData>('/article');
    articleData.data.id;
  })();
}